## EUP Replication Code
##
## Look Who's Talking: Parliamentary Debate in the European Union
##
## 12 November 2009
##
## Jonathan B. Slapin, slapinj@tcd.ie
## Sven-Oliver Proksch, proksch@uni-mannheim.de


rm(list=ls(all=TRUE)) 
library(Zelig)
library(xtable)

#Load data - Make sure working directory is appropriately set before loading
load("EUPrepdata.Rdata")


##################################
#### INDIVIDUAL ANALYSIS EP6 #####
################################## 



attach(ep6data)


### TABLE 4: Proportion of MEPs Giving a Speech as a Function of 
### Voting and Candidate Selection Mechanisms 
### (Vote-speech sample from 6th EP, 2004-2005) 
	
	table(natrebel,centralizedcandidateselection,gavespeech)

### TABLE 4: two-sample test of proportions

	t.test(gavespeech[natrebel==1 & centralizedcandidateselection==0],
	gavespeech[natrebel==0 & centralizedcandidateselection==0],alternative="two.sided",
	var.equal=TRUE,conf.level=0.95)

	t.test(gavespeech[natrebel==1 & centralizedcandidateselection==1],
	gavespeech[natrebel==0 &centralizedcandidateselection==1],alternative="two.sided",
	var.equal=TRUE,conf.level=0.95)

### WEBAPPENDIX Table 1: Relationship between Voting Decisions
### and Legislative Speeches (Vote-speech sample from 6th EP, 2004-2005) 
	
	table(natrebel,gavespeech)


#### WEBAPPENDIX Table 1: two-sample test of proportions
	
	t.test(gavespeech[natrebel==0],gavespeech[natrebel==1],
	alternative="two.sided",var.equal=TRUE,conf.level=0.95)

	
detach(ep6data)



#################################
#### AGGREGATE ANALYSIS EP5 #####
################################# 


attach(ep5data)


######## TABLE 5: Negative Binomial MODEL A
model.A<-zelig(epwebsitespeechcount~ pernpepg+ perepgnp+  tenure+absent+rapporteurshipcount+ partyleader +epleader+ numcommem+ numcomleader+groupsize+ natpartyperc +candidateselection+  pernpepg:candidateselection,model="negbin",data= ep5data ,save.data = TRUE)
summary(model.A)


######### TABLE 5: Negative Binomial MODEL B: includes memberstate and epgroup dummies
model.B<-zelig(epwebsitespeechcount~ pernpepg+perepgnp+   tenure+absent+rapporteurshipcount+ partyleader +epleader+ numcommem+ numcomleader+natpartyperc+candidateselection+  pernpepg:candidateselection+ factor(memberstate)+factor(epg),model="negbin",data=ep5data,save.data = TRUE)
summary(model.B)


######### TABLE 6: Simulate first differences for MODEL A

	#### National rebel: low score, Candidate selection: decentralized
	
	x.lowrebel.decentralizedCS<-setx(model.A, 
		pernpepg =quantile(model.A$zelig.data$pernpepg,0.1),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=0)
		s.out1<-sim(model.A,x=x.lowrebel.decentralizedCS)
		summary(s.out1) 
	
	#### National rebel: high score, Candidate selection: decentralized	
	x.highrebel.decentralizedCS<-setx(model.A, 
		pernpepg = quantile(model.A$zelig.data$pernpepg,0.9),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=0)
		s.out2<-sim(model.A,x=x.highrebel.decentralizedCS)
		summary(s.out2)
	
	#### National rebel: low score, Candidate selection: centralized
	
	x.lowrebel.centralizedCS<-setx(model.A, 
		pernpepg =quantile(model.A$zelig.data$pernpepg,0.1),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
		s.out3<-sim(model.A,x=x.lowrebel.centralizedCS)
		summary(s.out3)
	
	#### National rebel: high score, Candidate selection: centralized
		
	x.highrebel.centralizedCS<-setx(model.A, 
		pernpepg = quantile(model.A$zelig.data$pernpepg,0.9),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)	
		s.out4<-sim(model.A,x=x.highrebel.centralizedCS)
		summary(s.out4)


#########
######### WEBAPPENDIX TABLE 2: ROBUSTNESS CHECKS
#########


	######## Negative Binomial MODEL A with robust SE
	model.A.robust<-zelig(epwebsitespeechcount~ pernpepg+ perepgnp+  tenure+absent+rapporteurshipcount+ partyleader +epleader+ numcommem+ numcomleader+groupsize+ natpartyperc +candidateselection+  pernpepg:candidateselection,model="negbin",data= ep5data ,save.data = TRUE,robust=TRUE)
summary(model.A.robust)


	######## Negative Binomial MODEL A without candidate selection
	model.A.noselec<-zelig(epwebsitespeechcount~ pernpepg+ perepgnp+  tenure+absent+rapporteurshipcount+ partyleader +epleader+ numcommem+ numcomleader+groupsize+ natpartyperc ,model="negbin",data= ep5data ,save.data = TRUE)
summary(model.A.noselec)

	######## Negative Binomial MODEL B without candidate selection
	model.B.noselec<-zelig(epwebsitespeechcount~ pernpepg+perepgnp+   tenure+absent+rapporteurshipcount+ partyleader +epleader+ numcommem+ numcomleader+natpartyperc+ factor(memberstate)+factor(epg),model="negbin",data=ep5data,save.data = TRUE)
summary(model.B.noselec)


#########
######### WEBAPPENDIX TABLE 3: Simulated first differences 
#########


#### National rebel voting defection
x.low<-setx(model.A, 
		pernpepg =quantile(model.A$zelig.data$pernpepg,0.1),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem= median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
		
	x.high<-setx(model.A, 
		pernpepg =quantile(model.A$zelig.data$pernpepg,0.9),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem= median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
	
	s.out<-sim(model.A,x=x.low)
	s.out1<-sim(model.A,x=x.high)
	s.out.diff<-sim(model.A,x=x.low,x1=x.high)
	summary(s.out.diff)


#### Candidate selection
x.low<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem= median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=0)
		
	x.high<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem= median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
	
	s.out<-sim(model.A,x=x.low)
	s.out1<-sim(model.A,x=x.high)
	s.out.diff<-sim(model.A,x=x.low,x1=x.high)
	summary(s.out.diff)


#### European Rebel Voting Defection
	x.low<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp= quantile(model.A$zelig.data$perepgnp,0.1),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
		
	x.high<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp= quantile(model.A$zelig.data$perepgnp,0.9),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
	
	s.out<-sim(model.A,x=x.low)
	s.out1<-sim(model.A,x=x.high)
	s.out.diff<-sim(model.A,x=x.low,x1=x.high)
	summary(s.out.diff)
	


#### EP Tenure
	x.low<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure= quantile(model.A$zelig.data$tenure,0.1),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
		
	x.high<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure= quantile(model.A$zelig.data$tenure,0.9),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
	
	s.out<-sim(model.A,x=x.low)
	s.out1<-sim(model.A,x=x.high)
	s.out.diff<-sim(model.A,x=x.low,x1=x.high)
	summary(s.out.diff)
	


#### Voting Absences
	x.low<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent= quantile(model.A$zelig.data$absent,0.1),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
		
	x.high<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent= quantile(model.A$zelig.data$absent,0.9),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
	
	s.out<-sim(model.A,x=x.low)
	s.out1<-sim(model.A,x=x.high)
	s.out.diff<-sim(model.A,x=x.low,x1=x.high)
	summary(s.out.diff)
	


#### Number of rapporteurships
	x.low<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=quantile(model.A$zelig.data$rapporteurshipcount,0.1),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
		
	x.high<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount= quantile(model.A$zelig.data$rapporteurshipcount,0.9),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
	
	s.out<-sim(model.A,x=x.low)
	s.out1<-sim(model.A,x=x.high)
	s.out.diff<-sim(model.A,x=x.low,x1=x.high)
	summary(s.out.diff)

	
#### Party group leader
	x.low<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
		
	x.high<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=1,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
	
	s.out<-sim(model.A,x=x.low)
	s.out1<-sim(model.A,x=x.high)
	s.out.diff<-sim(model.A,x=x.low,x1=x.high)
	summary(s.out.diff)



#### EP leadership
	x.low<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
		
	x.high<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=1,
		numcommem=median(model.A$zelig.data$numcommem),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
	
	s.out<-sim(model.A,x=x.low)
	s.out1<-sim(model.A,x=x.high)
	s.out.diff<-sim(model.A,x=x.low,x1=x.high)
	summary(s.out.diff)
	
#### Number of committee assignments
	x.low<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem= quantile(model.A$zelig.data$numcommem,0.1),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
		
	x.high<-setx(model.A, 
		pernpepg =mean(model.A$zelig.data$pernpepg),
		perepgnp=mean(model.A$zelig.data$perepgnp),
		tenure=mean(model.A$zelig.data$tenure),
		absent=mean(model.A$zelig.data$absent),
		rapporteurshipcount=median(model.A$zelig.data$rapporteurshipcount),
		partyleader=0,
		epleader=0,
		numcommem= quantile(model.A$zelig.data$numcommem,0.9),
		numcomleader=median(model.A$zelig.data$numcomleader),
		groupsize= mean(model.A$zelig.data$groupsize),
		natpartyperc =mean(model.A$zelig.data$natpartyperc),
		candidateselection=1)
	
	s.out<-sim(model.A,x=x.low)
	s.out1<-sim(model.A,x=x.high)
	s.out.diff<-sim(model.A,x=x.low,x1=x.high)
	summary(s.out.diff)
	
	
	
detach(ep5data)